var LoginPage=React.createClass({
	componentDidMount:function(){
		if(cookie.getCookie("studioId")!=""){
			window.location.href="index.html";
		}
        if (window.localStorage) {  
            if (window.localStorage.userName && window.localStorage.password) { 
                document.getElementById('remember').checked = true;
            }
            $("#userNameInput").val(window.localStorage.userName);
            $("#passWordInput").val(window.localStorage.password);
        }
	},
	login:function(){
		$("#loginError").text("");
		var userName=$("#userNameInput").val();
		var password=$("#passWordInput").val();
		if (document.getElementById('remember').checked && window.localStorage) { 
            window.localStorage.userName = userName;
            window.localStorage.password = password;
        } else {    
            window.localStorage.clear();
        }
        $.ajax({
			url:config.api.login[0],
			type:config.api.login[1],
			data:{
				email:userName,
				password:password
			},
			success:function(res){
				cookie.setCookie("studioId",res.studio.studioId);
				window.location.href="index.html";
			},
			error:function(e){
				if(e.responseJSON.err==5){
					//账号密码错误
					$("#loginError").text("账号或密码错误");
				}else{
					$("#loginError").text("未知错误");
				}
				
			}
		});
	},
	handleLoginKeyDown: function(event) {
		if (event.keyCode != 13) { // entry key
			return;
		}
		this.login();
	},
	render:function(){
		return (
		<div className="loginBox">
            <div id="logo"> 
                <img src="image/浏览器logo.png" id="logo"/> 
                <h3>美术帮</h3>
            </div>
			<input type="text" className="loginInput" id="userNameInput" placeholder="输入邮箱"/>
			<input type="password" className="loginInput" id="passWordInput" placeholder="输入密码" onKeyDown={this.handleLoginKeyDown}/>
			<div id="rememberPW"><input type='checkbox' id="remember"/>记住密码</div>
            <p id="loginError"></p>
			<div className="loginBtn" onClick={this.login}></div>
			<a href="register.html" id="registerBtn">注册</a>
            <a href="forget.html" id="forgetBtn">忘记密码</a>
		</div>
			);
	}
});
React.render(
  <LoginPage/>,
  document.getElementById('container')
);
